package com.project.petLossSystem.controller.admin;

import com.project.petLossSystem.common.ServiceResultEnum;
import com.project.petLossSystem.po.PetCategory;
import com.project.petLossSystem.service.PetCategoryService;
import com.project.petLossSystem.util.PageQueryUtil;
import com.project.petLossSystem.util.Result;
import com.project.petLossSystem.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class PetCategoryController {

    @Autowired
    private PetCategoryService petCategoryService;

    @GetMapping("/petCategories")
    public String petCategory() {
        return "/pc/admin/pet_categories";
    }

    @GetMapping("/petCategories/list")
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("page")) || ObjectUtils.isEmpty(params.get("rows")) || ObjectUtils.isEmpty(params.get("parentId"))) {
            return ResultGenerator.failureResult("参数异常！");
        }
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        // 因为是表格用的数据，所以不写失败
        return ResultGenerator.successResult(petCategoryService.getCategoryPage(pageQueryUtil));
    }

    @PostMapping("/petCategories/save")
    @ResponseBody
    public Result save(@RequestBody PetCategory petCategory) {
        // 判断必须参数
        if (!StringUtils.hasText(petCategory.getCategoryName()) ||
                ObjectUtils.isEmpty(petCategory.getCategoryLevel()) ||
                ObjectUtils.isEmpty(petCategory.getParentId())) {
            return ResultGenerator.failureResult("参数异常！");
        }
        // 结果判断
        String resultMsg = petCategoryService.insertCategory(petCategory);
        if (resultMsg.equals(ServiceResultEnum.SUCCESS.getResult())) {
            return ResultGenerator.successResult();
        } else {
            return ResultGenerator.failureResult(resultMsg);
        }
    }

    @PutMapping("/petCategories/update")
    @ResponseBody
    public Result update(@RequestBody PetCategory petCategory) {
        // 判断必须参数，不必要分类等级，因为前端修改无法修改等级
        if (ObjectUtils.isEmpty(petCategory.getCategoryId()) ||
                !StringUtils.hasText(petCategory.getCategoryName()) ||
                ObjectUtils.isEmpty(petCategory.getParentId())) {
            return ResultGenerator.failureResult("参数异常！");
        }
        String resultMsg = petCategoryService.updateCategory(petCategory);
        if(resultMsg.equals(ServiceResultEnum.SUCCESS.getResult())){
            return ResultGenerator.successResult();
        }else return ResultGenerator.failureResult(resultMsg);
    }

    // 删除需要删除旗下所有子分类
    @PutMapping ("/petCategories/delete")
    @ResponseBody
    public Result delete(@RequestBody Long[] categoryIds){
        // 判断参数，如果是null是映射不上的，只需判断长度
        if(categoryIds.length < 1){
            return ResultGenerator.failureResult("参数异常！");
        }
        PetCategory firstCategory = petCategoryService.getCategoryById(categoryIds[0]);
        // 这里记得得new，因为数组直接转的list是数组的内部类，不是util下的
        List<Long> idList = new ArrayList<>(Arrays.asList(categoryIds)); // 删除的全存
        // 一级分类
        if(firstCategory.getCategoryLevel() == 1){
            // 获取二级分类
            List<Long> secondIdList = petCategoryService.getIdListByParentIds(idList); // 无数据是空集合不是null
            idList.addAll(secondIdList);
        }
        // 更新deleted
        if(petCategoryService.updateDeleted(idList)){
            return ResultGenerator.successResult();
        }else return ResultGenerator.failureResult("删除失败！");
    }

}
